<?php
require_once('mysql.php');
require_once('user.class.php');

class News{
	
	var $id;
        var $author;
	var $title;
	var $content;
	var $timestamp;
        
        public function __construct($id_tmp,$id_author,$title_tmp,$content_tmp,$timestamp_tmp) {
            $this->id=$id_tmp;
            $this->title=$title_tmp;
            $this->content=$content_tmp;
            $this->timestamp=$timestamp_tmp;
            
            $bdd = new Mysql();
            $query = "SELECT * FROM user WHERE user.id='".$id_author."'";
            if (!$result = $bdd->Send_Query($query))
                    die($bdd->return_error ());
            else {
                $row = $bdd -> get_array($result);
                $this->author = new User($row['id'],$row['pseudo'],$row['mail'],$row['nom'],$row['prenom']);
                $bdd->close();
            }
        }
        
        public function getId() {
            return $this->id;
        }

       /* public function getId_author() {
            return $this->id_author;
        }*/

        public function getTitle() {
            return $this->title;
        }

        public function getContent() {
            return  $this->content;
        }

        public function getTimestamp() {
            return 'Le '.date('j M y',$this->timestamp).' à '.date('G\hi',$this->timestamp).' ';
        }
        
        public function getAuthor() {
            return $this->author;
        }

                
        public function getJSON(){
            $string='{';
            $string.='"id_news":"'.$this->getId().'"';
            $string.=',';
            $string.='"id_author":"'.$this->getAuthor()->getUsageName().'"';
            $string.=',';
            $string.='"title":"'.$this->getTitle().'"';
            $string.=',';
            $string.='"content":"'.$this->getContent().'"';
            $string.=',';
            $string.='"date":"'.$this->getTimestamp().'"';
            $string.='}';
            
            return $string;
        }
        
        
        public function getHTML(){
            
            $toolbox = new ToolBox();
            
            $template = '<div id="%%ID_NEWS%%" class="news">
                        <h2>%%TITRE%%</h2>
                        <span class="date">%%DATE%% par %%AUTEUR%%</span><br/><br/>
                        <div class="news_content"> %%NEWS_CONTENT%%</div>
                        </div>';
         
            $template = str_replace('%%ID_NEWS%%', 'news_'.$this->getID(), $template);
            $template = str_replace('%%TITRE%%',$toolbox->strip_double_quote($this->getTitle()), $template);
            $template = str_replace('%%DATE%%',$this->getTimestamp(), $template);
            $template = str_replace('%%AUTEUR%%',$this->getAuthor()->getUsageName(), $template);
            $template = str_replace('%%NEWS_CONTENT%%',$toolbox->strip_double_quote($this->getContent()), $template);
            
            return $template;
        }
	
}

?>